/*
 * Task 1. Prime Numbers
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
 * Class gives the output of prime numbers which are smaller or equal to the inputted from keyboard
 *integer number
 *
 * @version 0.2 24 Jan 2013
 * @author Tymur Lysak
 */

public class PrimeNumbers {

	private LinkedList<Integer> primes = new LinkedList<Integer>();

	public static void main(String[] args) {
		PrimeNumbers programPrimes = new PrimeNumbers();
		int enteredNumber = getNumberFromKeyboard("Enter integer for searching smaller or equal prime numbers");
		List primes = programPrimes.getListOfPrimes(enteredNumber);
		programPrimes.outputPrimesToPromt(primes);
	}
	
	public static int getNumberFromKeyboard(String messageToUser) {
		System.out.println(messageToUser);
		return new Scanner(System.in).nextInt();
	}
	
	public List<Integer> getListOfPrimes(int enteredNumber) { //Searching prime numbers 
		LinkedList<Integer> listOfPrimes = new LinkedList<Integer>();
		if (enteredNumber >= 2) {
			listOfPrimes.add(new Integer(2));
		}
		int commonNumber = 2;
		while (commonNumber <= enteredNumber) {
			boolean help = true;
			int j = 0;

			while (j < listOfPrimes.size()) {

				if ((commonNumber % listOfPrimes.get(j)) == 0) {
					help = false;
					break;
				}
				j++;
			}
			if (help)
				listOfPrimes.add(commonNumber);
			commonNumber++;
		}
		return listOfPrimes;
	}
	public void outputPrimesToPromt(List<Integer> resultedList) { //Outputting the Array of searched prime numbers
		System.out.println("The list of primes:");
		Iterator<Integer> it = resultedList.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}
